package com.workflow.engine.eb.steps.jd.login;

import com.workflow.engine.core.flow.StepState;
import com.workflow.engine.parser.constant.HttpMethod;
import com.workflow.engine.parser.flow.impl.AbstractStep;
import com.workflow.engine.parser.utils.JsoupHelper;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static com.workflow.engine.parser.constant.HttpMethod.GET;
import static com.workflow.engine.parser.utils.StepStateUtil.successfulStepState;

/**
 * 京东登录
 *
 * Created by Administrator on 2017/6/7.
 */

//@PGConfig({
//        @PGScheme(requestParamName = "utm_term", strategy = StrategyType.CONTEXT, strategyNeedParams = { "utm_term", ""}),
//        @PGScheme(requestParamName = "cu", strategy = StrategyType.CONTEXT, strategyNeedParams = {"cu", ""}),
//        @PGScheme(requestParamName = "utm_campaign", strategy = StrategyType.CONTEXT, strategyNeedParams = { "utm_campaign", ""}),
//        @PGScheme(requestParamName = "utm_medium", strategy = StrategyType.CONTEXT, strategyNeedParams = { "utm_medium", ""}),
//        @PGScheme(requestParamName = "utm_source", strategy = StrategyType.CONTEXT, strategyNeedParams = {"utm_source", ""}),
//})
public class JDJumpToIndex extends AbstractStep {

    private static final Logger logger = LoggerFactory.getLogger(JDJumpToIndex.class);

    @Override
    public Logger getLogger() {
        return logger;
    }

    @Override
    protected String getRequestUrl() {
        return "https://www.jd.com/";
//        return "https://home.jd.com/respond.proxy.js?_="+System.currentTimeMillis();
    }

    @Override
    protected HttpMethod getHttpMethod() {
        return GET;
    }

    @Override
    protected Map<String, String> getHeaders() {
        return new HashMap<String, String>(){{
            put("Host", "www.jd.com");
            put("Connection", "keep-alive");
            put("Upgrade-Insecure-Requests", "1");
            put("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
            put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            put("Referer", "https://passport.jd.com/new/login.aspx");
            put("Accept-Encoding", "gzip, deflate, sdch, br");
            put("Accept-Language", "zh-CN,zh;q=0.8");
        }};
    }

    @Override
    protected StepState handleResponse(Map<String, Object> context, Object response) throws Exception {
        logger.info("title is {}", JsoupHelper.getElementsByTagName(Jsoup.parse((String)response), "title"));

        return successfulStepState(this);
    }

}
